package Bacterias;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.SoundManager;
import com.thauanLopes.bacterium.GameSceneView;
import com.thauanLopes.bacterium.GameManages.ScoreManager;
import com.thauanLopes.smagasmaga.R;

public class Bacteria extends GameObject {

	private String TAG = "Bacteria NORMAL";
	private List<GameObject> cemetery;
	private List<GameObject> gameObjects;

	private int ScoreValue = 1;

	private double tempoStagio = 0.7f;
	private int stagio = 0;
	private int stagios = 3;

	private int hitsToDie = 1;
	private int hitCounter = 0;

	public void setHitsToDie(int hitsToDie) {
		this.hitsToDie = hitsToDie;
	}

	private boolean alive = true;

	private int distanciaDoTeto = 30;

	public double getDeseaseTime() {
		return tempoStagio;
	}

	public void setDeseaseTime(double deseaseTime) {
		this.tempoStagio = deseaseTime;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	private double cronometro;

	private Rect rect;
	private int x;
	private int y;

	private GameSceneView gameSceneView;

	private Bitmap bitmap;

	public Bacteria(List<GameObject> gameObjects, List<GameObject> cemetery,
			GameSceneView gameSceneView) {

		this.cemetery = cemetery;
		this.gameObjects = gameObjects;
		this.gameSceneView = gameSceneView;

		this.bitmap = BitmapStagio(this.stagio);

		int tmpX = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasWidth() - bitmap.getWidth() - 2
						* (MinhasCoisas.getCanvasWidth() / 10))
				+ (MinhasCoisas.getCanvasWidth() / 10);
		int tmpY = MinhasCoisas.getRandom().nextInt(
				MinhasCoisas.getCanvasHeight() - bitmap.getHeight() - 2
						* (MinhasCoisas.getCanvasHeight() / 10))
				+ MinhasCoisas.getCanvasHeight() / 10;

		this.rect = new Rect(tmpX, tmpY, tmpX + bitmap.getWidth(), tmpY
				+ bitmap.getHeight());

		boolean deu = false;
		for (int i = 0; i < 3; i++) {

			boolean da = true;
			for (GameObject gameObject : gameObjects) {
				if (gameObject.getClass().equals(Bacteria.class)) {
					Bacteria gos = (Bacteria) gameObject;
					if (this.rect.intersect(gos.rect)) {
						da = false;
						break;
					}
				}
			}
			if (da) {
				deu = da;
				break;
			}
		}

		if (!deu) {
			alive = false;
		} else {

			setPosition(tmpX, tmpY);

			gameObjects.add(this);
		}
	}

	@Override
	public void Update(double deltaTime) {
		// TODO Auto-generated method stub
		if (alive) {
			if (cronometro < tempoStagio) {
				cronometro += deltaTime;

			} else {

				if (stagio >= stagios) {
					gameSceneView.setLost(true);
				} else {
					stagio++;
					this.bitmap = BitmapStagio(stagio);
				}

				cronometro = 0;
			}
		}
	}

	@Override
	public void Draw(Canvas canvas) {
		if (alive) {
			canvas.drawBitmap(bitmap, x, y, MinhasCoisas.getPaint());
		}
	}

	@Override
	public void Die() {
		this.cemetery.add(this);
		this.alive = false;
		SoundManager.getInstance().playSound(R.raw.pop_2);
		ScoreManager.getInstance().AddScore(this.ScoreValue);

	}

	@Override
	public void Destroy() {
		gameObjects.remove(this);
	}

	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
		this.rect.set(x, y, x + this.bitmap.getWidth(),
				y + this.bitmap.getHeight());
	}

	@Override
	public boolean onTouch(MotionEvent event) {

		float fingerX = event.getRawX();
		float fingerY = event.getRawY();

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			if (this.rect.contains((int) fingerX, (int) fingerY)) {
				this.hitCounter++;
				if (this.hitCounter >= this.hitsToDie) {
					this.Die();
				}
				return true;
			}
			break;

		}
		return false;
	}

	private Bitmap BitmapStagio(int stagio) {

		Bitmap sorteada = null;
		switch (stagio) {
		case 0:
			sorteada = BitmapStorage.getInstance().getBacteria_verde();
			break;
		case 1:
			sorteada = BitmapStorage.getInstance().getBacteria_roxa();
			break;

		case 2:
			sorteada = BitmapStorage.getInstance().getBacteria_vermelha();
			break;
		case 3:
			sorteada = BitmapStorage.getInstance().getBacteria_vermelha();
			break;

		}

		return sorteada;
	}

}
